/*
 	Olsz
 	
 	Authors:
 	Daniel Moore <moore.daniel.f@gmail.com>
 	
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
#===========================================================================*/
public class Olsz {
	private static StringBuffer BINARY;
	private static int DECIMAL;

	public Olsz(){						//Default constructor
		BINARY.setLength(0);
		DECIMAL = 0;
	} 
	
	public Olsz(StringBuffer _BIN){		//Binary conversion to decimal constructor 
		boolean isBinary = false;
		
		isBinary = bitScanner(_BIN);
		
		if (isBinary == true){	
			BINARY = _BIN;
			DECIMAL = toDECIMAL(_BIN);
		}
			
	} 
	public Olsz(int _DEC){				//Decimal conversion to binary constructor
		
		if (_DEC >= 0){					//Checks to see if _DEC is greater than or equal to zero
			BINARY = toBINARY(_DEC);
			DECIMAL = _DEC;
		}
	} 
	
	public static StringBuffer getBINARY() {
		return BINARY;
	}

	public static int getDECIMAL() {
		return DECIMAL;
	}
	
	private static boolean bitScanner(StringBuffer str){
													
		boolean isBinary = false;	
		for(int i = 0; i < str.length(); i++){			//Loops through the binary string
			char bit = str.charAt(i);						//and evaluates the selected bit 
			if(bit != '1' && bit != '0'){
				isBinary = false;
				return isBinary;			
			} else
				isBinary = true;
		}//End of for loop
		
		return isBinary;		
	}
	
	public static int toDECIMAL(StringBuffer str) {
		BINARY = str;
		int DECIMAL = 0;
		int RANGE_bit = str.length();
			
		for (int i = 0; i <= (RANGE_bit -1); i++){
			char _ON = str.charAt(i);
			
			if (_ON == '1'){
				int X = getBitValues((RANGE_bit - 1) - i); 
				DECIMAL = DECIMAL + X; 
			}
		}		
		return DECIMAL;
	}

	public static StringBuffer toBINARY(int _DEC){
		StringBuffer str = new StringBuffer();
		int[] arr = new int[2];
		int topStack = 0;
		int range = 0;
		
		if (_DEC == 0){
			str.append("0");
			return str;
		}
			
		arr = getDECIMAL_Properties(_DEC);
		range = (arr[0] - 1);
		topStack = _DEC;
		
		for (int i = range; i >= 0; i--){
			int bitValue = getBitValues(i);
			
			if (topStack >= bitValue){
				str.append("1");
				int mod = topStack % bitValue;
				topStack = mod;
			}else
				str.append("0");
		}
		
		return str;
	}
	
	private static int[] getDECIMAL_Properties(int _DEC) {
		int RANGE_bit = 0;
		int MAX_bitVALUE = 0;
		int[] arr;
        arr = new int[2];      
		
		for(int i = 0; _DEC >= MAX_bitVALUE; i++){
			MAX_bitVALUE = getBitValues(i);				
			RANGE_bit = i;
		}
				
		arr[0] = RANGE_bit;
		arr[1] = MAX_bitVALUE;
		
		return arr;
	}

	public static int getBitValues(int range) {		//Finds the binary value of a single bit  
		int value = (int) Math.pow(2, range);     		// by raising 2 by the power of the bit's range
		return value;										// (i.e. 2^7 = 128 )  
	}
}
